package view.create.behind {
	import com.greensock.TweenMax;
	
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	import flash.text.TextFieldAutoSize;
	
	import subumbo.leg.model.Lib;
	
	public class Skin extends MovieClip {
		
		private var _outline:Shape;
		private var _centered:Boolean;
		private var _upload:MovieClip;
		private var _instructionText:MovieClip;
		private var _file:FileReference;
		private var _loader:Loader;
		
		private var _loaded:Boolean;
		
		public var guide:MovieClip;
		
		public function init(centered:Boolean=false):void {
			_loader = new Loader();
			_file = new FileReference();
			_centered = centered;
			addChild(_loader);
			_outline = new Shape();
			addChild(_outline);
			_upload = Lib.getMovieClip("Button");
			_upload.txt.autoSize = TextFieldAutoSize.LEFT;
			_upload.txt.text = "upload";
			_upload.bg.width = _upload.txt.width + 50;
			_upload.buttonMode = true;
			_upload.mouseChildren = false;
			_instructionText = Lib.getMovieClip("InstructionText");
			if(!centered) {
				_upload.x = width * .5 - _upload.width * .5;
				_upload.y = height * .5 - _upload.height * .5;	
			} else {
				_upload.x = -_upload.width * .5;
				_upload.y = -_upload.height * .5;
			}
			addChild(_upload);
			addChild(_instructionText);
			
			uploadEnabled = true;
		}
		
		private function onUpload(e:MouseEvent):void {
			_file.addEventListener(Event.SELECT, onFileSelected, false, 0 , true);
			var swfTypeFilter:FileFilter = new FileFilter("SWF Files (*.swf)","*.swf");
			var imageTypeFilter:FileFilter = new FileFilter("Images", "*.jpg;*.png");
			_file.browse([swfTypeFilter, imageTypeFilter]);
		}
		
		private function onFileSelected(e:Event):void {
			_file.removeEventListener(Event.SELECT, onFileSelected);
			_file.addEventListener(Event.COMPLETE, onFileLoaded);
			_file.load();
		}
		
		private function onFileLoaded(e:Event):void {
			_file.removeEventListener(Event.COMPLETE, onFileLoaded);
			_loader.loadBytes(_file.data);
		}
		
		public function set instructionText(val:String):void {
			_instructionText.txt.width = this.width - 20;
			_instructionText.txt.autoSize = TextFieldAutoSize.CENTER;
			
			if(!_centered) {
				_instructionText.y = this.height * .5 - _upload.height - 40;
			} else {
				_instructionText.x  = - 50
				_instructionText.y = -_upload.height - 20;
			}
			_instructionText.txt.text = val;
		}
		
		public function set uploadEnabled(val:Boolean):void {
			if(val) {
								
				with(_outline.graphics) {
					clear();
					lineStyle(5,0x999999);
					beginFill(0xFFFFCD,.9);
					if(!_centered) drawRoundRect(-10, -10, width - 25, height + 20, 50, 50);
					else drawRoundRect(-10 - width/2, -10 - height/2, width + 20, height + 20, 50, 50);
				}
				addEventListener(MouseEvent.ROLL_OUT, onMouse);
				addEventListener(MouseEvent.ROLL_OVER, onMouse);
				addEventListener(MouseEvent.MOUSE_DOWN, onUpload);
				_outline.alpha = 0;
				_upload.alpha = 0;
				_instructionText.alpha = 0;
				guide.alpha = 1;
			}else {
				_outline.graphics.clear();
				if(_loaded)guide.alpha = 0;
				removeEventListener(MouseEvent.ROLL_OUT, onMouse);
				removeEventListener(MouseEvent.ROLL_OVER, onMouse);
				removeEventListener(MouseEvent.MOUSE_DOWN, onUpload);
			}
		}
		
		
		
		private function onMouse(e:MouseEvent):void {
			switch(e.type) {
				case MouseEvent.ROLL_OVER : 
					TweenMax.to(_outline, .6, {alpha:1});
					TweenMax.to(_upload, .6, {alpha:1});
					TweenMax.to(_instructionText, .6, {alpha:1});
				break;
				
				case MouseEvent.ROLL_OUT : 
					TweenMax.to(_outline, .6, {alpha:0});
					TweenMax.to(_upload, .6, {alpha:0});
					TweenMax.to(_instructionText, .6, {alpha:0});
				break;
			}
		}
		
		public function get fileReference():FileReference {
			return  _file;
		}
	}
}